package com.lx.spring.config.config;


import com.lx.spring.config.bean.Yellow;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@PropertySource(value = "classpath:/db.properties")
@Configuration
public class ProfileConfig {

    @Value("${db.user}")
    private String user;

    private StringValueResolver valueResolver;

    @Value("${db.driverClass}")
    private String driverClass;


//    @Profile(value = "test")
    @Bean
    public Yellow yellow(){
        return new Yellow();
    }

    // 设置激活指定配置 方式一 vm 运行时参数 -Dspring.profiles.active=dev
//    @Profile(value = "dev")
    @Bean(value = "dev-datasource")
    public DataSource dataSourceDev(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://47.107.169.231:19505/clouddb99");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile(value = "test")
    @Bean(value = "test-datasource")
    public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://47.107.169.231:19505/clouddb02");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile(value = "prod")
    @Bean(value = "prod-datasource")
    public DataSource dataSourceProd(@Value("${db.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://47.107.169.231:19505/clouddb03");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }



}
